package com.google.gsaguide.gplusbridge;

import android.content.Context;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.NetworkResponse;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gsaguide.volley.AppController;

import org.json.JSONObject;
import org.json.XML;

/**
 * This class allows to get a response of a PHP Script that returns the user's information.
 * @author Felipe Porge Xavier - http://www.felipeporge.com
 */
public class GPlusRequestHelper implements Response.ErrorListener, Response.Listener<String> {

    private final String DEBUG_TAG = getClass().getSimpleName();

    public static final int UNKNOWN_ERROR = -1;
    public static final int NO_ERROR = 0;
    public static final int PARSE_ERROR = 1;

    private Context mContext;
    private AppController mAppController;

    private OnGPlusRequestListener mOnGPlusRequestListener;

    public GPlusRequestHelper(Context context, OnGPlusRequestListener onGPlusRequestListener) {
        mContext = context;
        mOnGPlusRequestListener = onGPlusRequestListener;
        mAppController = AppController.getInstance();
    }

    public void getInfo(String id) {
//      String requestURL  = "http://www.felipeporge.com/scripts/gplus_bridge.php?id=" + id
//                + "&key=" + mContext.getString(R.string.api_key);
//      String requestURL = "https://www.googleapis.com/plus/v1/people/" +
//                    id + "?fields=id,displayName,image&key=" + mContext.getString(R.string.api_key);
        String requestURL = "http://picasaweb.google.com/data/entry/api/user/" + id;

        StringRequest stringRequest = new StringRequest(requestURL, this, this);
        stringRequest.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS,
                5,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        mAppController.addToRequestQueue(stringRequest, id);
    }

    @Override
    public void onErrorResponse(VolleyError volleyError) {
        int errorCode = UNKNOWN_ERROR;
        NetworkResponse response = volleyError.networkResponse;
        if (response != null && response.data != null) {
            errorCode = response.statusCode;
        }
        mOnGPlusRequestListener.onPostExecute(null, errorCode);
    }

    @Override
    public void onResponse(String str) {
        // This code snippet clean the data of the request result.
        try {
            JSONObject picasaJson = XML.toJSONObject(str).getJSONObject("entry");
            JSONObject result = new JSONObject();
            result.put("id", picasaJson.getString("gphoto:user"));
            result.put("image", picasaJson.getString("gphoto:thumbnail").replace("/s64-c/", "/s200-c/"));
            result.put("displayName", picasaJson.getString("gphoto:nickname"));
            mOnGPlusRequestListener.onPostExecute(result, NO_ERROR);
        } catch (Exception e) {
            mOnGPlusRequestListener.onPostExecute(null, PARSE_ERROR);
        }
    }

    // This method allows to cancel all the pending requests.
    public void cancelAllPedingRequests() {
        mAppController.cancelAllPendingRequests();
    }
}

